package com.atguigu.meinian.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.meinian.constant.MessageConstant;
import com.atguigu.meinian.pojo.TravelGroup;
import com.atguigu.meinian.service.TravelGroupService;
import com.atguigu.meinian.vo.PageResult;
import com.atguigu.meinian.vo.QueryPageBean;
import com.atguigu.meinian.vo.R;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author zbystart
 * @create 2021-06-21 14:39
 */
@RestController
@RequestMapping("/travelgroup")
public class TravelGroupController {

    @Reference
    private TravelGroupService travelGroupService;

    @PostMapping("/list")
    public PageResult list(@RequestBody QueryPageBean queryPageBean) {
        return travelGroupService.list(queryPageBean);
    }


    @PostMapping("/add")
    public R add(@RequestBody TravelGroup travelGroup) {
        try {
            travelGroupService.add(travelGroup);
            return R.ok(MessageConstant.ADD_TRAVELGROUP_SUCCESS,null);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(MessageConstant.ADD_TRAVELGROUP_FAIL);
        }
    }

    @PostMapping("/update")
    public R update(@RequestBody TravelGroup travelGroup) {
        try {
            travelGroupService.update(travelGroup);
            return R.ok(MessageConstant.EDIT_TRAVELGROUP_SUCCESS,null);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(MessageConstant.EDIT_TRAVELGROUP_FAIL);
        }
    }

    @GetMapping("/show")
    public R show(Integer id) {
        try {
            Map<String,Object> map = travelGroupService.queryById(id);
            return R.ok(MessageConstant.QUERY_TRAVELGROUP_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(MessageConstant.QUERY_TRAVELGROUP_FAIL);
        }
    }

    @GetMapping("/delete")
    public R delete(Integer id) {
        try {
            travelGroupService.deleteById(id);
            return R.ok(MessageConstant.DELETE_TRAVELGROUP_SUCCESS,null);
        } catch (RuntimeException e) {
            return R.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(MessageConstant.DELETE_TRAVELGROUP_FAIL);
        }
    }
}
